package chaw.android.train.MoreActivities;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import chaw.android.train.R;
import chaw.android.util.BoardItem;
import chaw.android.util.HttpRequestManager;
import chaw.android.util.ServerURL;
import chaw.android.util.TimeCheck;
import android.app.ListActivity;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class ModifyMyStoryActivity extends ListActivity{
	private BoardAdapter nadapter;
	private ImageView ok;
	private ImageView btnClose;
	
	@Override
	public void onCreate(Bundle savedInstanceState){
		super.onCreate(savedInstanceState);
        setContentView(R.layout.modify_my_story);
		ok = (ImageView)findViewById(R.id.btnOk);
		btnClose = (ImageView) findViewById(R.id.btnClose);

		ok.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		}); 
		
		btnClose.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				finish();
			}
		});
		setListView();
	}
	
	@SuppressWarnings("unchecked")
	private void setListView(){
		Intent intent = getIntent();
		ArrayList<BoardItem> boardList = (ArrayList<BoardItem>) intent.getExtras().get("modify_list");
		nadapter = new BoardAdapter(getBaseContext(), R.layout.modify_station_board_row, boardList);
	    ListView lv = getListView();
	    lv.setAdapter(nadapter);
	}

	private class BoardAdapter extends ArrayAdapter<BoardItem>{
		private ArrayList<BoardItem> items;
		private int rsrc;
		
		public BoardAdapter(Context ctx, int rsrcId, ArrayList<BoardItem> data){
			super(ctx, rsrcId, data);
			this.items = data;
			this.rsrc = rsrcId;
		}
		
		@Override
		public View getView(final int position, View convertView, ViewGroup parent){
			View v = convertView;
			if(v == null){
				LayoutInflater li = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
				v = li.inflate(rsrc, null);
			}

			final BoardItem e = items.get(position);
			if(e != null){
				String title = e.getTitle();
				if(title.length() > 14){
					title = title.substring(0, 13) + "..";
				}
				((TextView)v.findViewById(R.id.board_title)).setText(title);

				ImageView image = ((ImageView)v.findViewById(R.id.user_pic));
				if(e.getImage() == null ){
					image.setImageResource(R.drawable.icon_nophoto_b);
				}
				else{
					image.setImageBitmap(e.getImage());
				}
				ImageView i = (ImageView)v.findViewById(R.id.theme1);
				if(e.getMeet().equals("1")){
					i.setImageResource(R.drawable.icon_theme_meet);
					i = nextIcon(v,i);
				}
				if(e.getRestaurant().equals("1")){
					i.setImageResource(R.drawable.icon_theme_food);
					i = nextIcon(v,i);
				}
				if(e.getPlace().equals("1")){
					i.setImageResource(R.drawable.icon_theme_tour);
					i = nextIcon(v,i);
				}
				if(e.getLodge().equals("1")){
					i.setImageResource(R.drawable.icon_theme_lodge);
					i = nextIcon(v,i);
				}
				if(e.getEtc().equals("1")){
					i.setImageResource(R.drawable.icon_theme_etc);
					i = nextIcon(v,i);
				}
				v = iconReset(v, i);
				
				((ImageButton)v.findViewById(R.id.btnDelete)).setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						items.remove(position);
						UploadAsynkTask task = new UploadAsynkTask();
						String[] params = new String[]{ ServerURL.deleteBoard, "board_id", e.getId()};
						task.execute(params);
						nadapter.notifyDataSetChanged();
					}
				});
			}
			return v;
		}
		
	}

	public ImageView nextIcon(View v, ImageView i){
		if(i.equals(v.findViewById(R.id.theme1))){
			i = (ImageView)v.findViewById(R.id.theme2);
		}
		else if(i.equals(v.findViewById(R.id.theme2))){
			i = (ImageView)v.findViewById(R.id.theme3);
		}
		else if(i.equals(v.findViewById(R.id.theme3))){
			i = (ImageView)v.findViewById(R.id.theme4);
		}
		else if(i.equals(v.findViewById(R.id.theme4))){
			i = (ImageView)v.findViewById(R.id.theme5);
		}
		return i;
	}
	public View iconReset(View v, ImageView i){
		if(i.equals(v.findViewById(R.id.theme1))){
			((ImageView)v.findViewById(R.id.theme1)).setImageDrawable(null);
			i = (ImageView)v.findViewById(R.id.theme2);
		}
		if(i.equals(v.findViewById(R.id.theme2))){
			((ImageView)v.findViewById(R.id.theme2)).setImageDrawable(null);
			i = (ImageView)v.findViewById(R.id.theme3);
		}
		if(i.equals(v.findViewById(R.id.theme3))){
			((ImageView)v.findViewById(R.id.theme3)).setImageDrawable(null);
			i = (ImageView)v.findViewById(R.id.theme4);
		}
		if(i.equals(v.findViewById(R.id.theme4))){
			((ImageView)v.findViewById(R.id.theme4)).setImageDrawable(null);
			i = (ImageView)v.findViewById(R.id.theme5);
		}
		if(i.equals(v.findViewById(R.id.theme5))){
			((ImageView)v.findViewById(R.id.theme5)).setImageDrawable(null);
		}
		return v;
	}
	private class UploadAsynkTask extends AsyncTask<String, Void, String> {
		@Override
		protected String doInBackground(String... urls) {
			// TODO Auto-generated method stub
			String response = null;
			
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			
			if(urls.length > 1)
				for(int i = 1; i< urls.length; i+=2)
					params.add(new BasicNameValuePair(urls[i], urls[i+1]));
			response = HttpRequestManager.request(urls[0], params);
			return response;
		}
	}
}
